package com.zxh.blog.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ImmersionBar
import com.tencent.smtt.sdk.WebView
import com.zxh.blog.R

/**
 *  文章详情页面
 */
class ArticleDetailActivity : AppCompatActivity() {

    private lateinit var mWebView: WebView

    private var mBaseURL = "http://www.xiaohongshare.xyz/#/DetailArticle?aid="

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_article_detail)

        ImmersionBar.with(this)
            .statusBarColor(R.color.app_back)
            .statusBarDarkFont(true)
            .fitsSystemWindows(true)
            .init()

        mWebView = findViewById(R.id.article_detail_webView)
        // 获取传递过来的id值
        val articleId = intent.getStringExtra("articleId")
        loadWebView(articleId)
        // 设置返回按钮点击事件
        val backButton = findViewById<ImageButton>(R.id.btn_back)
        backButton.setOnClickListener {
            // 创建返回结果的 Intent，并添加当前位置参数
            val resultIntent = Intent()
            setResult(Activity.RESULT_OK, resultIntent)
            finish() // 关闭当前页
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun loadWebView(articleId: String?) {
        mWebView.settings.javaScriptEnabled = true
        // 获取 WebView 设置对象
        val settings = mWebView.settings
        // 设置 WebView 的编码方式
        settings.defaultTextEncodingName = "UTF-8"
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        // 加载webView
        val url = "$mBaseURL$articleId"
        mWebView.loadUrl(url)
    }


}